package assist.client.gwt.view.activity;

import java.util.Set;

import com.google.web.bindery.event.shared.EventBus;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

import assist.client.gwt.service.PlayersProvider;
import assist.client.gwt.view.ClientFactory;
import assist.client.gwt.view.place.SelectStatsPlace;
import assist.client.gwt.view.place.ShowStatsPlace;
import assist.client.gwt.view.ui.SelectStatsView;
import assist.domain.model.Player;
import assist.domain.model.StatsQuery;
import assist.domain.model.StatsQuery.ShotType;
import assist.domain.model.StatsQuery.StatType;
import assist.domain.model.StatsQuery.Strength;


public class SelectStatsActivity extends StatsActivity implements
    SelectStatsView.Presenter 
{ 
    private PlayersProvider playersProvider; 
	public SelectStatsActivity(SelectStatsPlace place, ClientFactory clientFactory) {
		super(place, clientFactory, clientFactory.getSelectStatsView());
		this.statsProvider.reset();
        //TODO: Need to cache the PlayersProvider in the GamesProvider.
        //OR PlayerProvider has own cache, which it clears onGamesChanged.		
		this.playersProvider =  super.clientFactory.getDefaultPlayersProvider();

	}
	

	/**
	 * Invoked by the ActivityManager to start a new Activity
	 */
	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
	    SelectStatsView selectStatsView = super.clientFactory.getSelectStatsView();
	    //TODO: Just setPresenter, which will implement HasStatsProvider, HasPlayersProvider?
	    selectStatsView.setPresenter(this);
	    //selectStatsView.setStatsProvider(super.statsProvider);

	    
		containerWidget.setWidget(selectStatsView.asWidget());
	}

	/**
	 * Ask user before stopping this activity
	 */
	@Override
	public String mayStop() {
		//return "Please hold on. This activity is stopping.";
	    return null;
	}

	/**
	 * Navigate to a new Place in the browser
	 */
	public void goToShowStats() 
	{	  
	    ShowStatsPlace place = new ShowStatsPlace(
	            super.clientFactory.getGamesProvider().getGamesQuery(), 
	            super.statsQuery);
	    
	    super.clientFactory.getPlaceController().goTo(place);
	}

    @Override
    public void addColumn(ShotType shotType, Strength strength, StatType statType)
    {
        super.statsQuery.addColumn(new StatsQuery.Column(shotType, strength, statType));
        this.goToSelectStatsPlace();
        super.statsProvider.reset();
        //Columns CANNOT be updated by proivider, so we're notifying the view directly.
        //TODO: Need to make a listener in the porovider, so that columns CAN be updated by provider, 
        //which can then fire a columns changed event to all views.
     //   super.clientFactory.getSelectStatsView().setStatsProvider(super.statsProvider);
        //this.statsProvider.resetRows();
        
    }
    
    @Override
    public void setWowyRows(Player player, Set<Player> players)
    {
        //This adds the played with row.
        //Also need to add the played Without rows.
        super.statsQuery.setPlayerWowy(player, players);
        this.goToSelectStatsPlace();
        super.statsProvider.reset();
    }
    


    @Override
    public void setPlayerRows(Set<Player> players)
    {
        super.statsQuery.setPlayers(players);
        this.goToSelectStatsPlace();
        super.statsProvider.reset();
    }    
    
    /**
     * Called be any method that requires a refresh of the view, and wants
     * a token in the history.
     */
    private void goToSelectStatsPlace()
    {
        //Want to navigate within the same place.
        //Create the place and add token, but don't use controller to navigate.
        SelectStatsPlace place = new SelectStatsPlace(
                super.clientFactory.getGamesProvider().getGamesQuery(), 
                super.statsQuery);
        SelectStatsPlace.Tokenizer tokenizer = new SelectStatsPlace.Tokenizer();        
        History.newItem("SelectStatsPlace:" + tokenizer.getToken(place), false);
    }


    @Override
    public PlayersProvider getPlayersProvider()
    {
        return this.playersProvider;
    }

}
